﻿using WpfMvvmTemplatePrismHC.Common;
using WpfMvvmTemplatePrismHC.Tools.Helpers;

namespace WpfMvvmTemplatePrismHC.Views.Main
{
    public partial class MainWindowView
    {
        private readonly IRegionManager regionManager;

        public MainWindowView(IRegionManager regionManager)
        {
            InitializeComponent();
            this.regionManager = regionManager;

            LogHelper.WriteLog("MainWindowView 初始化完成！");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 加载配置文件

            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);

            switch (jObject?["AppSettings"]?["Theme"]!.ToString())
            {
                case "0": ((App)Application.Current).UpdateTheme(ApplicationTheme.Light); break;
                case "1": ((App)Application.Current).UpdateTheme(ApplicationTheme.Dark); break;
            }

            //语言
            GlobalStaticData.LanguageNum = (int)(jObject?["AppSettings"]?["Language"]!);

            //选择颜色
            JToken selectedBrushToken = jObject?["AppSettings"]?["SelectedBrush"]?.Value<JToken>()!;
            Brush accent = JsonConvert.DeserializeObject<Brush>(selectedBrushToken.ToString())!;
            ((App)Application.Current).UpdateAccent(accent);

            ComMethod.LanguageWinDicHelper(GlobalStaticData.LanguageNum, this);

            #endregion

            //区域默认导航到ViewA
            regionManager.Regions["ContentRegion"].RequestNavigate("ViewA");
        }


        #region 切换主题、语言、颜色

        private void ButtonConfig_OnClick(object sender, RoutedEventArgs e) => PopupConfig.IsOpen = true;

        private void ButtonSkins_OnClick(object sender, RoutedEventArgs e)
        {
            string selectBtnBrushStr = JsonConvert.SerializeObject(selectColorBtn.Background);

            if (e.OriginalSource is Button button)
            {
                PopupConfig.IsOpen = false;

                //颜色
                Brush? accent = null;

                //读取文件内容
                string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);
                JObject? jObject = JObject.Parse(fileContent);

                if (button.Tag is ApplicationTheme tag)
                {
                    //切换主题
                    ((App)Application.Current).UpdateTheme(tag);

                    JToken? jToken = jObject?["AppSettings"]?["Theme"];

                    switch (tag)
                    {
                        case ApplicationTheme.Light: jToken?.Replace(JToken.FromObject("0")); break;
                        case ApplicationTheme.Dark: jToken?.Replace(JToken.FromObject("1")); break;
                    }
                }
                else if (button.Tag is string numTag && int.TryParse(numTag, out int res))
                {
                    //解析文件内容
                    JToken? jToken = jObject?["AppSettings"]?["Language"];

                    //修改Language的值
                    jToken?.Replace(JToken.FromObject(res.ToString()!));

                    GlobalStaticData.LanguageNum = res;
                    ComMethod.LanguageWinDicHelper(GlobalStaticData.LanguageNum, this, false);

                    //提示弹框
                    MessageBox.Show(GlobalStaticData.LanguageDic["Switch languages after software restart!"], GlobalStaticData.LanguageDic["tisps2"]);
                }
                else
                {
                    if (button.Tag is Brush accentTag)
                    {
                        accent = accentTag;
                        ((App)Application.Current).UpdateAccent(accent);

                        switch (button.Name)
                        {
                            case "btn1": accent = new SolidColorBrush(Color.FromRgb(33, 150, 243)); break;
                            case "btn2": accent = new SolidColorBrush(Color.FromRgb(233, 175, 32)); break;
                            case "btn3": accent = new SolidColorBrush(Color.FromRgb(156, 39, 176)); break;
                            case "btn4": accent = new SolidColorBrush(Color.FromRgb(45, 184, 77)); break;
                        }
                    }
                    else if (button.Tag is "Picker")
                    {
                        var picker = SingleOpenHelper.CreateControl<ColorPicker>();
                        Brush connectBtnBrush = JsonConvert.DeserializeObject<Brush>(selectBtnBrushStr)!;
                        picker.SelectedBrush = (SolidColorBrush)connectBtnBrush;

                        var window = new PopupWindow
                        {
                            PopupElement = picker,
                            WindowStartupLocation = WindowStartupLocation.CenterScreen,
                            AllowsTransparency = true,
                            WindowStyle = WindowStyle.None,
                            MinWidth = 0,
                            MinHeight = 0,
                            Title = GlobalStaticData.LanguageDic["SelectAccentColor"],
                        };

                        //选择颜色
                        picker.SelectedColorChanged += (s, e) =>
                        {
                            accent = picker.SelectedBrush;
                            ((App)Application.Current).UpdateAccent(picker.SelectedBrush);
                        };

                        //取消按钮
                        picker.Canceled += (s, e) =>
                        {
                            // 恢复到初始默认的颜色
                            Brush connectBtnBrush = JsonConvert.DeserializeObject<Brush>(selectBtnBrushStr)!;
                            ((App)Application.Current).UpdateAccent(connectBtnBrush);
                            window.Close();
                        };

                        //确认按钮  
                        picker.Confirmed += (s, e) => { window.Close(); };
                        window.ShowDialog();
                    }

                    //解析文件内容
                    JToken? jToken = jObject?["AppSettings"]?["SelectedBrush"];
                    string json = JsonConvert.SerializeObject(accent);
                    jToken?.Replace(JToken.FromObject(json));

                }

                //保存中英文到配置文件中
                File.WriteAllText(GlobalStaticData.AppSettingsFilePath, jObject?.ToString());
            }
        }

        #endregion

    }
}